import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:wanke/check_password.dart';
import 'package:wanke/password_page.dart';
import 'function.dart';
import 'package:local_auth/local_auth.dart';
import 'package:local_auth/auth_strings.dart';
import 'package:device_info/device_info.dart';
import 'dart:io';
class SafePage extends StatefulWidget {
  SafePage(this.mainColor);
  final mainColor;
  @override
  _SafePageState createState() => new _SafePageState();
}

class _SafePageState extends State<SafePage> {
  bool gestureOn = false;
  bool fingerOn = false;
  int fingerSupport=0; //0不支持 1支持 2faceID
  DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
  @override
  void initState() {
    super.initState();
    initSafe();
    initDeviceInfo();
  }
  initDeviceInfo() async {
    if(Platform.isAndroid){
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      if(androidInfo.fingerprint!=null){
        fingerSupport=1;
      }
    }else if(Platform.isIOS){
      IosDeviceInfo iosInfo=await deviceInfo.iosInfo;
      String machine=iosInfo.utsname.machine;
      fingerSupport=iosSupportFingerPrint(machine);
    }
    setState(() {

    });
  }
  initSafe() async {
    this.gestureOn = await getStorage("gesture") != null;
    this.fingerOn = await getStorage("finger") != null;
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new CupertinoNavigationBar(
        actionsForegroundColor: Colors.white,
        backgroundColor: widget.mainColor,
        middle: new Text(
          "安全设置",
          style: new TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new ListTile(
              leading: new ImageIcon(
                AssetImage("images/gesture-password.png"),
              ),
              title: new Text("手势密码"),
              trailing: CupertinoSwitch(
                value: gestureOn,
                onChanged: (val) async {
                  if (val) {
                    bool gestureAuth = await Navigator.of(context).push(
                      new CupertinoPageRoute(builder: (context) {
                        return new PasswordPage(widget.mainColor);
                      }),
                    )??false;
                    gestureOn = gestureAuth;
                  } else {
                    bool gestureAuth = await Navigator.of(context).push(
                      new CupertinoPageRoute(builder: (context) {
                        return new CheckPassword(widget.mainColor,1);
                      }),
                    )??false;
                    gestureOn = !gestureAuth;
                    if(gestureAuth){
                      removeStorage("gesture");
                    }
                  }
                  setState(() {});
                },),
            ),
            new Divider(
              height: 1.0,
            ),
            fingerSupport!=0?new ListTile(
              leading: fingerSupport==2?new ImageIcon(AssetImage("images/faceID.png")):new Icon(Icons.fingerprint),
              title: new Text(fingerSupport==2?"FaceID验证":"指纹验证"),
              trailing: CupertinoSwitch(
                value: fingerOn,
                onChanged: (val) async {
                  var localAuth = new LocalAuthentication();
                  bool didAuthenticate = await localAuth.authenticateWithBiometrics(
                    localizedReason: fingerSupport==2?"请验证FaceID":'请验证指纹',
                    androidAuthStrings: new AndroidAuthMessages(
                      cancelButton: "取消",
                      goToSettingsButton: "设置指纹",
                      goToSettingsDescription: "需先设置指纹后开启",
                      fingerprintHint: "验证指纹",
                      fingerprintSuccess: "指纹验证成功",
                      signInTitle: "请验证指纹",
                      fingerprintNotRecognized: "指纹无效，请重新验证",
                      fingerprintRequiredTitle: "尚未设置指纹",
                    ),
                    iOSAuthStrings: new IOSAuthMessages(
                      cancelButton: '取消',
                      goToSettingsButton: '设置',
                      goToSettingsDescription:fingerSupport==2?"需先设置FaceID后开启": '需先设置指纹后开启',
                      lockOut: fingerSupport==2?"请重新验证FaceID":'请重新验证指纹',
                    ),
                  );
                  if (val) {
                    fingerOn = didAuthenticate;
                    if(didAuthenticate){
                      setStorage("finger", "on");
                    }
                  } else {
                    fingerOn = !didAuthenticate;
                    if(didAuthenticate){
                      removeStorage("finger");
                    }
                  }
                  setState(() {});
                },),
            ):new Container(),
            fingerSupport!=0?new Divider(
              height: 1.0,
            ):new Container(),
          ],
        ),
      ),
    );
  }
}
